package com.service.Authorization.controller;

import com.service.Authorization.dto.UserExtendDto;
import com.service.Authorization.service.PortalUserExtendService;
import com.service.config.utils.RedisUtil;
import com.service.config.utils.RegexUtil;
import com.service.config.utils.Result;
import com.service.config.utils.UserTokenManager;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

import static com.service.config.constant.Constant.*;

/**
 * 用户信息扩展(PortalUserExtend)表控制层
 *
 * @author makejava
 * @since 2021-08-10 16:14:29
 */
@RestController
@RequestMapping("portalUserExtend")
@Api(value = "租户用户信息扩展接口",tags = "租户用户信息扩展接口")
public class PortalUserExtendController {
    /**
     * 服务对象
     */
    @Resource
    private PortalUserExtendService portalUserExtendService;
    @Resource
    private RedisUtil redisUtil;

    /**
     * 用户完善信息
     *
     * @param
     * @return
     */
    @PostMapping(value = "perfectUserInfo",produces="application/json")
    @ApiOperation(value = "用户完善信息")
    @ResponseBody
    public Result perfectUserInfo(@RequestHeader(value = "cloud-Auth-Token") String cloudAuthToken,
                                  @RequestBody UserExtendDto userExtendDto) throws Exception {
        if (cloudAuthToken != null && !cloudAuthToken.equals("")) {
            String userId = UserTokenManager.getUserId(cloudAuthToken);
            if (!RegexUtil.isMobileSimple(userExtendDto.getTelephone())) {
                return Result.failure("手机号格式错误，请重新校验");
            }
            if (redisUtil.exist(cloudAuthToken+userId)) {
                userExtendDto.setUserId(userId);
                portalUserExtendService.insertUserExtend(userExtendDto);
                return Result.ok();
            }else {
                return Result.failure(USERNAME_Off_SITE,usernameOffSite);
            }
        }
        return Result.failure(USER_NOT_LOGIN,"用户未登录");
    }
    /**
     * 获取用户信息
     *
     * @param
     * @return
     */
    @GetMapping(value = "getUserInfo",produces="application/json")
    @ApiOperation(value = "获取用户信息")
    @ResponseBody
    public Result getUserInfo(@RequestHeader(value = "cloud-Auth-Token") String cloudAuthToken,
                              @RequestParam("serviceId") String serviceId) throws Exception {
        if (cloudAuthToken != null && !cloudAuthToken.equals("")) {
            String userId = UserTokenManager.getUserId(cloudAuthToken);
            if (redisUtil.exist(cloudAuthToken+userId)) {
                return Result.ok().setData(portalUserExtendService.queryByUserId(userId));
            }else {
                return Result.failure(USERNAME_Off_SITE,usernameOffSite);
            }
        }
        return Result.failure(USER_NOT_LOGIN,"用户未登录");
    }
}
